// pages/article/article.js
Page({

    /**
     * 页面的初始数据
     */
    data: {
        activity: {},
        id: null
    },
    participateClick() {
        const that = this
        wx.request({
          url: getApp().globalData.backUrl + '/request/saveUserAndActivity',
          method: 'POST',
          data: {
              userId: that.data.id,
              activityId: that.data.activity._id
          },
          success(res) {
            if (res.statusCode === 200 && res.data.code === 200) {
                wx.request({
                    url: getApp().globalData.backUrl + '/request/saveIntegral',
                    method: 'POST',
                    data: {
                      userId: getApp().globalData.userInfo._id,
                      type: '参加活动',
                      integral: getApp().globalData.userInfo.integral + 2,
                      message: '活动名称：' + that.data.activity.activityName,
                      record: '+2',
                      time: new Date().toISOString()
                    },
                    success(res) {
                      if (res.statusCode === 200 && res.data.code === 200) {
                        getApp().globalData.userInfo.integral = getApp().globalData.userInfo.integral + 2
                        wx.showToast({
                            title: '积分保存成功',
                            icon: 'success'
                          })
                          wx.navigateTo({url: '/pages/info/info'+'?type=success&info=报名成功',})
                      }else {
                        wx.showToast({
                          title: '积分保存失败',
                          icon: 'error'
                        })
                      }
                    }
                  }) 
            }else {
              wx.navigateTo({url: '/pages/info/info'+'?type=success&info='+res.data.message,})
            }
          }
        })
    },
    // 导出报名信息
    exportActivity(e) {
      wx.request({
        url: getApp().globalData.backUrl + '/request/getExportActivity',
        method: 'POST',
        data: { 
          activityId: e.currentTarget.dataset.activityid
        },
        success(res) {
          if (res.statusCode === 200 && res.data.code === 200) {
            wx.setClipboardData({
              data: res.data.excelUrl,
              success(res) {
                wx.showModal({
                  cancelColor: 'cancelColor',
                  title: '下载链接已经复制，请到浏览器打开',
                })
              }
            })
          }else {
            wx.showToast({
              title: res.data.message,
              icon: 'none'
            })
          }
        },
        fail(err) {
          console.log(err);
        }
      })
    },
    /**
     * 生命周期函数--监听页面加载
     */
    onLoad: function (options) {
        this.setData({activity: JSON.parse(options.activity),id: getApp().globalData.userInfo._id})
    },

    /**
     * 生命周期函数--监听页面初次渲染完成
     */
    onReady: function () {

    },

    /**
     * 生命周期函数--监听页面显示
     */
    onShow: function () {

    },

    /**
     * 生命周期函数--监听页面隐藏
     */
    onHide: function () {

    },

    /**
     * 生命周期函数--监听页面卸载
     */
    onUnload: function () {

    },

    /**
     * 页面相关事件处理函数--监听用户下拉动作
     */
    onPullDownRefresh: function () {

    },

    /**
     * 页面上拉触底事件的处理函数
     */
    onReachBottom: function () {

    },

    /**
     * 用户点击右上角分享
     */
    onShareAppMessage: function () {

    }
})